import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import PhysPassLock from "./PhysPassLock";
import Sound from "../../../../constant/SoundNameConstant";
import { utils } from "../../../../utils/CommonUtil";

export default class PhysPassKey extends InteractiveBase {
    /** 当前旋转索引*/
    public curIndex: number = 0;

    private curPassLock: PhysPassLock;

    /** 是否旋转x*/
    public isRotateX: boolean = false;
    /** 是否旋转y*/
    public isRotateY: boolean = false;
    /** 每次旋转角度*/
    public rotateAngle: number;
    /** 索引上界*/
    private indexMax: number;

    private mowner: Laya.Sprite3D;

    private childAtParentIndex = 0;

    private isRotateOver = true;
    
    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.curPassLock = this.owner.parent.getComponent(PhysPassLock) as PhysPassLock;
        if (this.curPassLock == null) {
            this.curPassLock = this.owner.parent.parent.getComponent(PhysPassLock) as PhysPassLock;
        }
        this.indexMax = Math.floor((360 / this.rotateAngle));

        for (let index = 0; index < this.curPassLock.owner.numChildren; index++) {
            if(this.owner.name == this.curPassLock.owner.getChildAt(index).name){
                this.childAtParentIndex = index;
                break;
            }
        }
    }
    protected InteractiveLogic(): boolean {
        if (this.curPassLock.isCorrect) {
            return false;
        }
        if(!this.isRotateOver){
            return false;
        }
        //  if (curTween != null && curTween.IsActive())
        //  {
        //      return false;
        //  }
        /** 当前的旋转角度*/
        let curRotateAngle: number = 0;
        let tempLocalRotationEuler = this.mowner.transform.localRotationEuler.clone() as Laya.Vector3;
        if (this.isRotateX) {
            curRotateAngle = tempLocalRotationEuler.x;
        }
        else if (this.isRotateY) {
            curRotateAngle = tempLocalRotationEuler.y;
        }
        else {
            curRotateAngle = tempLocalRotationEuler.z;
        }

        let targetAngle: Laya.Vector3 = new Laya.Vector3(0, 0, 0);
        if (this.isRotateX) {
            curRotateAngle -= this.rotateAngle;
            curRotateAngle = curRotateAngle % 360;
            targetAngle = new Laya.Vector3(curRotateAngle, 0, 0);
        }
        else if (this.isRotateY) {
            curRotateAngle += this.rotateAngle;
            curRotateAngle = curRotateAngle % 360;
            targetAngle = new Laya.Vector3(0, curRotateAngle, 0);
        }
        else {
            curRotateAngle += this.rotateAngle;
            curRotateAngle = curRotateAngle % 360;
            targetAngle = new Laya.Vector3(0, 0, curRotateAngle);
        }
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.physPassLock);

        console.log("targetAngle:",targetAngle);
        this.isRotateOver = false;
        utils.CommonUtils.DORotateTween(
            this.mowner.transform,
            this.mowner.transform.localRotationEuler
            ,targetAngle,200,Laya.Handler.create(this,()=>{
                this.curPassLock.CheckPassWord();
                this.isRotateOver = true;

        }),true,false,false)
        /** 增加当前的索引*/
        this.curIndex = (this.curIndex + 1) % this.indexMax;
        this.curPassLock.ChildPassKeyCurIndex[this.childAtParentIndex] = this.curIndex;
        return false;
    }
}